package cn.zmit.kuxi.activity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

/** * @author @parameter * @return */
public class UserActivity extends BaseActivity {
	@ViewInject(R.id.tv_set)
	private TextView tv_set;

	@ViewInject(R.id.imag_user_pic)
	public static ImageView imag_user_pic;

	@ViewInject(R.id.tv_my_user_name)
	private TextView tv_my_user_name;

	@ViewInject(R.id.tv_kubi_money)
	private TextView tv_kubi_money;

	@ViewInject(R.id.tv_my_user_phone_num)
	private TextView tv_my_user_phone_num;

	@ViewInject(R.id.rl_join_record)
	private RelativeLayout rl_join_record;

	@ViewInject(R.id.rl_win_record)
	private RelativeLayout rl_win_record;

	@ViewInject(R.id.rl_recharge)
	private RelativeLayout rl_recharge;

	@ViewInject(R.id.rl_select)
	private RelativeLayout rl_select;

	@ViewInject(R.id.tl_user_data)
	private RelativeLayout tl_user_data;
	@ViewInject(R.id.rl_info)
	public static RelativeLayout rl_info;
	@ViewInject(R.id.btn_login)
	public static Button btn_login;
	@ViewInject(R.id.rl_login)
	private RelativeLayout rl_login;
	@ViewInject(R.id.tv_login)
	private TextView tv_login;
	public static String userid, name, phone, pic, id, kubi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		ViewUtils.inject(this);
		initVIew();

	}

	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);

			JsonData jsonData = JsonData.create(response);
			JsonData data = jsonData.optJson("user_info");
			if (data != null && data.length() > 0) {
				id = data.optString("id");
				name = data.optString("name");
				pic = data.optString("avatar");
				kubi = data.optString("credit");
				phone = data.optString("phone");
				tv_my_user_name.setText("昵称：" + name);
				if (pic != null) {
					ImageDisplayer.getInstance().dispalyImageWithRadius(
							Url.IMAGR_ROOT + pic, imag_user_pic, 0);
				} else {
					imag_user_pic.setImageResource(R.drawable.user_pic_def);
				}
				tv_kubi_money.setText("酷币：" + kubi);
				tv_my_user_phone_num.setText("手机号：" + phone);
			}
		}

	}

	private void initVIew() {
		Boolean has_login = PreferenceHelper.readBoolean(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
				false);
		if (has_login) {
			userid = PreferenceHelper.readString(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", userid);
			RequestTask.getInstance().doUserinfo(this, map,
					new onRequestListener());
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (PreferenceHelper.readBoolean(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
			rl_login.setVisibility(View.GONE);
			rl_info.setVisibility(View.VISIBLE);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", userid);
			RequestTask.getInstance().doUserinfo(this, map,
					new onRequestListener());

		} else {
			rl_login.setVisibility(View.VISIBLE);
			rl_info.setVisibility(View.GONE);
			imag_user_pic.setImageResource(R.drawable.user_pic_def);
		}

	}

	@OnClick({ R.id.btn_exit, R.id.tv_set, R.id.imag_user_pic,
			R.id.tv_my_user_name, R.id.tv_kubi_money,
			R.id.tv_my_user_phone_num, R.id.rl_join_record, R.id.rl_win_record,
			R.id.rl_recharge, R.id.rl_select, R.id.tl_user_data, R.id.tv_login,
			R.id.rl_send })
	private void OnClick(View v) {
		switch (v.getId()) {
		case R.id.imag_user_pic:

			break;
		case R.id.tv_login:// 登陆
			openActivity(LoginActivity.class);

			break;
		case R.id.tv_set:// 设置
			openActivity(SetActivity.class);

			break;

		case R.id.rl_join_record:// 参与记录
			if (PreferenceHelper.readBoolean(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
				openActivity(JoinRecordActivity.class);
			} else {
				ToastUtils.show(context, "登陆后可查看信息……");
				openActivity(LoginActivity.class);
			}

			break;
		case R.id.rl_win_record:// 中奖记录
			if (PreferenceHelper.readBoolean(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
				openActivity(WinningRecordActivity.class);
			} else {
				ToastUtils.show(context, "登陆后可查看信息……");
				openActivity(LoginActivity.class);
			}
			break;
		case R.id.rl_recharge:// 充值记录
			if (PreferenceHelper.readBoolean(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
				Bundle bundle = new Bundle();
				bundle.putString("name", name);
				bundle.putString("pic", pic);
				bundle.putString("kubi", kubi);
				bundle.putString("phone", phone);

				openActivity(RechargeRecordActivity.class, bundle);
			} else {
				ToastUtils.show(context, "登陆后可查看信息……");
				openActivity(LoginActivity.class);
			}

			break;
		case R.id.rl_select:// 快递查询
			if (PreferenceHelper.readBoolean(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
				openActivity(ExpressActivity.class);
			} else {
				ToastUtils.show(context, "登陆后可查看信息……");
				openActivity(LoginActivity.class);
			}

			break;
		case R.id.tl_user_data:// 个人资料
			if (PreferenceHelper.readBoolean(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED)) {
				Bundle bundle = new Bundle();
				bundle.putString("name", name);
				bundle.putString("phone", phone);
				bundle.putString("userPic", pic);
				bundle.putString("userId", userid);
				openActivity(UserInfoActivity.class, bundle);
			} else {
				ToastUtils.show(context, "登陆后可查看信息……");
				openActivity(LoginActivity.class);
			}

			break;
		case R.id.rl_send:// send
			// send crash.log
//			Log.e("send===", "click");
//			File file = new File(Environment.getExternalStorageDirectory(),
//					"crash.log");
//			Log.e("file===", file + "");
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("crash", file);
//			RequestTask.getInstance().doSendCrash(getApplicationContext(), map,
//					new SendCrashListener());

			break;

		default:
			break;
		}

	}

	private class SendCrashListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			Log.e("crash===", "ok!");
			Toast.makeText(context, "senddone", 0).show();
			// 出现异常把app干掉重启而不是卡死在界面
			// android.os.Process.killProcess(android.os.Process.myPid());
		}
	}
}
